package user

import (
	"github.com/gin-gonic/gin"
	util2 "go-api-template/internal/util"
)

func (req *SaveReq) Bind(ctx *gin.Context) (err error) {
	err = ctx.ShouldBindJSON(&req)
	if err != nil {
		return
	}

	// Mail check
	// if req.Email != "" {
	// 	ok, _ = regexp.Match(e2.RegularMail,
	// 		[]byte(req.Email))
	// 	if !ok {
	// 		err = errors.New("邮箱格式错误")
	// 	}
	// }

	// 默认密码
	if req.Password == "" {
		req.Password = "123456"
	}
	// 默认头像
	// if req.Avatar == "" {
	// }
	return
}

func (req *ListReq) Bind(ctx *gin.Context) (err error) {
	err = ctx.ShouldBindJSON(req)
	if err != nil {
		return
	}

	// Default Page
	if req.Page == 0 {
		req.Page = 1
	}
	if req.PageSize == 0 {
		req.PageSize = 10
	}

	// Default Remove Space
	req.Username = util2.DeletePreAndSufSpace(req.Username)
	req.Email = util2.DeletePreAndSufSpace(req.Email)

	return
}
